﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Configuration;
using System.Linq;
using System.Web.Configuration;
using System.IO;
using System.Web;
using System.Web.UI;


namespace Lswweb.Controls
{
    public class RenderViewControl<T> where T : UserControl
    {
        #region 构造 and 字段

        private Page viewControlHolder;
        private HttpContext context;
        private T skinControl;

        public RenderViewControl(string filePath)
            : this(filePath, HttpContext.Current) { }

        public RenderViewControl(string filePath, HttpContext context)
            : this(null, filePath, context) { }

        public RenderViewControl(T control)
            : this(control, HttpContext.Current) { }

        public RenderViewControl(T control, HttpContext context)
            : this(control, string.Empty, context) { }

        private RenderViewControl(T control,string skinFilePath, HttpContext context)
        {
            this.context = context;
            this.viewControlHolder = RenderViewControl<T>.GetHolderPage();
            this.viewControlHolder.Controls.Clear();

            this.skinControl = control == null
                ? (T)this.viewControlHolder.LoadControl(skinFilePath)
                : control;   
      
            
        }

        #endregion

        public T SkinControl
        {
            get { return this.skinControl; }
        }

        public string RenderControl()
        {
            HttpContext.Current = this.context;

            using (StringWriter output = new StringWriter())
            {
                this.viewControlHolder.Controls.Add(this.skinControl);
                this.viewControlHolder.Server.Execute(this.viewControlHolder, output, false);

                return output.ToString();
            }
        }

        #region static methods

        public static string RenderControl(T control)
        {
            RenderViewControl<T> viewControl = new RenderViewControl<T>(control);

            return viewControl.RenderControl();
        }

        /// <summary>
        /// UserControl.Page 是在Page.LoadControl(filePath)时将Page对象赋值给UserControl
        /// 所以在用户控件ascx页面中调用的Page对象就是在这里赋予的
        /// </summary>
        private static Page page;
        private static Page GetHolderPage()
        {
            if (RenderViewControl<T>.page == null)
            {
                string pageBaseTypeFullName = ((PagesSection)ConfigurationManager.GetSection("system.web/pages")).PageBaseType;
                if (string.IsNullOrEmpty(pageBaseTypeFullName))
                {
                    RenderViewControl<T>.page = new System.Web.UI.Page();
                }
                else
                {
                    RenderViewControl<T>.page = (Page)Activator.CreateInstance(Type.GetType(pageBaseTypeFullName));
                }
            }

            return RenderViewControl<T>.page;
        }

        #endregion
    }
}
